<?php

class CategoryController extends \BaseController {

    /**
     * 获得全部栏目
     * @return [array] [栏目名，模型名，模型id]
     * $categories['categoryId']    栏目id
     * $categories['name']  栏目名
     * $categories['modelId']       模型id
     * $categories['modelName']     模型名
     */
    public function index(){
        $categories = CategoryService::index();
        return View::make('category.index')->with('categories', $categories);
    }

    /**
     * 进入新增栏目页面
     * @return [array] [模型信息]
     * $models['id']
     * $models['name']
     * $models['table_name']
     */
    public function create(){
        $models = Model::all();
        return View::make('category.add')->with('models', $models);
    }

    /**
     * 增加栏目
     * @param [string] $name    栏目名
     * @param [int] $modelId            所用模型id
     * @return 异常信息
     */
    public function add(){
        // $name  = Input::get('name');
        // $modelId       = Input::get('modelId');
        $data        = Input::all();
        $rules = array('name' => 'required | alpha_dash | unique:category,name',
                        'modelId'     => 'required | exists:model,id');
        BaseService::validateInputParam($data, $rules);
        $result = CategoryService::add($data);
        return $result;
    }

    /**
     * 进入栏目更新字段
     * @return [array] [栏目id、栏目名，模型信息]
     * category 栏目信息：id, name, model_id
     * model 模型信息 id, name, table_name
     */
    public function edit(){
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId' => $categoryId);
        $rules      = array('categoryId' => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $category   = Category::find($categoryId);
        if(!isset($category)){
            return Response::json(new NoCategoryException);
        }
        $models = Model::all();
        $data = array('category' => $category, 'models' => $models);
        return View::make('category.update')->with(array('category' => $category, 'models' => $models));
    }

    /**
     * 更新栏目
     * @return [array] [异常信息]
     */
    public function update(){
        // $name   = Input::get('name');
        // $modelId        = Input::get('modelId');
        // $categoryId     = Input::get('categoryId');
        // $data  = array('name' => $name, 'modelId' => $modelId, 'categoryId' => $categoryId);
        $data = Input::all();
        $rules = array('name' => 'required',
                        'modelId'     => 'required | exists:model,id',
                        'id'  => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $result = CategoryService::update($data);
        return $result;
    }

    /**
     * 删除栏目
     * @return [array] [异常信息]
     */
    public function delete(){
        $id = Input::get('id');
        $data       = array('id' => $id);
        $rules      = array('id' => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $result = CategoryService::delete($id);
        return $result;
    }
}